<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // Promise.any()的语法和all也完全相同

    // then返回的是最先成功的那个的结果

    // 当所有的promise实例都出错的时候，会走catch。返回“AggregateError: All promises were rejected”



    function require(url) {
      return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.send();
        xhr.onload = function() {
          if (xhr.status === 200) {
            // console.log(JSON.parse(xhr.responseText)); // 数据一
            // resolve(JSON.parse(xhr.responseText))

            reject(999)
          } else {
            console.error('请求失败', xhr.status);
          }
        };
      })
    }

    function delay(ms) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          // resolve(1234)
          reject(5678)
        }, ms)
      })
    }


    const p1 = require('https://cnodejs.org/api/v1/topics');
    const p2 = require('https://cnodejs.org/api/v1/topic/61f69e4aa08b39f75309c2a8');
    const p3 = delay(3);
    Promise.any([p1, p2, p3]).then(res => {
      console.log(res);
    }).catch(err => {
      console.log(err);
      
    })
  </script>
</body>
</html>